using Microsoft.AspNetCore.Mvc;
using WebRtcProject.Models;
using WebRtcProject.Services;

namespace WebRtcProject.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class RoomsController : ControllerBase
    {
        private readonly IRoomService _roomService;
        private readonly ILogger<RoomsController> _logger;

        public RoomsController(IRoomService roomService, ILogger<RoomsController> logger)
        {
            _roomService = roomService;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有房间列表
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<List<Room>>> GetRooms()
        {
            try
            {
                var rooms = await _roomService.GetAllRoomsAsync();
                return Ok(rooms);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error getting rooms");
                return StatusCode(500, "Internal server error");
            }
        }

        /// <summary>
        /// 根据ID获取房间信息
        /// </summary>
        [HttpGet("{id}")]
        public async Task<ActionResult<Room>> GetRoom(string id)
        {
            try
            {
                var room = await _roomService.GetRoomAsync(id);
                if (room == null)
                {
                    return NotFound($"Room with ID {id} not found");
                }

                return Ok(room);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error getting room {RoomId}", id);
                return StatusCode(500, "Internal server error");
            }
        }

        /// <summary>
        /// 创建新房间
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<Room>> CreateRoom([FromBody] CreateRoomRequest request)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request.Name))
                {
                    return BadRequest("Room name is required");
                }

                var room = await _roomService.CreateRoomAsync(request.Name, request.MaxUsers);
                return CreatedAtAction(nameof(GetRoom), new { id = room.Id }, room);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error creating room");
                return StatusCode(500, "Internal server error");
            }
        }

        /// <summary>
        /// 删除房间
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteRoom(string id)
        {
            try
            {
                var success = await _roomService.DeleteRoomAsync(id);
                if (!success)
                {
                    return NotFound($"Room with ID {id} not found");
                }

                return NoContent();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error deleting room {RoomId}", id);
                return StatusCode(500, "Internal server error");
            }
        }

        /// <summary>
        /// 获取房间用户列表
        /// </summary>
        [HttpGet("{id}/users")]
        public async Task<ActionResult<List<User>>> GetRoomUsers(string id)
        {
            try
            {
                var users = await _roomService.GetRoomUsersAsync(id);
                return Ok(users);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error getting users for room {RoomId}", id);
                return StatusCode(500, "Internal server error");
            }
        }
    }

    public class CreateRoomRequest
    {
        public string Name { get; set; } = string.Empty;
        public int MaxUsers { get; set; } = 10;
    }
} 